<?php
/*
 MRSS.INC.PHP by martin $ 2009/12/12 01:25:31
*/

$db = DB.'images_linked.db';
$root = ROOT_PATH;
$ppblog_version = PPBLOG_VERSION;
$logo = file_exists(PATH.$theme_dir.'Images/ImageGallery.png') ? $root.$theme_dir.'Images/ImageGallery.png' : '';
if($logo) $logo = NL.'<atom:icon>'.$logo.'</atom:icon>'.NL;

if(is_file($db)){
 $images = file($db);
} else return;
$_images = array();

foreach ($images as $img){
 if(substr($img, 10, 1)=='d' || strpos($img, '.png')) continue;
 $_images[] = $img;
}
$images = $_images;

$total = sizeof($images);
$step = 100;
$pagesMRSS = ceil($total / $step);

for ($i = 0; $i < $pagesMRSS; $i++){
 $_images = array_slice($images, $i * $step, $step);
 $prev = ($i > 0) ? NL.'<atom:link rel="previous" href="'.$root.'feeds/gallery'.$i.'.rss" />': '';
 $next = (($i + 1) < $pagesMRSS) ? NL.'<atom:link rel="next" href="'.$root.'feeds/gallery'.($i + 2).'.rss" />' : '';
 $ITEMS = '';

 foreach ($_images as $info){
  $split = explode("|", $info); 
  $len = sizeof($split);// IMG: 9, 12
  $id = preg_replace('{^0+}', '', $split[0]); $name = $split[2]; $title = $split[7];
  if(strpos($name, '.jpg')){
   $type = 'image/jpeg';
  } elseif(strpos($name, '.png')){
   $type = 'image/png';
  } elseif(strpos($name, '.bmp')){
   $type = 'image/bmp';
  } else $type = '';
  if(strpos($title, ',')){
   list(,$title) = explode(',', $title);
   $link = MODE_STATIC ? $root.date('Ym', $id).'-'.$title.EXT_STATIC : $root.'index.php?UID='.$id;
  } else {
   list($title,) = explode('.', $name);
   $link = MODE_STATIC ? $root.$id.EXT_STATIC : $root.'index.php?UID='.$id;
  }
  $ITEMS .= NL.'<item>
 <title>'.$title.' - '.date('Y/m/d', $id).'</title>
 <link>'.$link.'</link>
 <guid>'.$id.'_'.$name.'</guid>
';
  if($len==9){ // No thumbnail
   $ITEMS .= ' <media:thumbnail url="'.$root.IMG_DIR.$id.'_'.$name.'" />
 <media:content url="'.$root.IMG_DIR.$id.'_'.$name.'" />
';
  } else {     // Thumbnail exists
   $ITEMS .= ' <media:thumbnail url="'.$root.IMG_DIR.THUMB1.$id.'_'.$name.'" />
 <media:content url="'.$root.IMG_DIR.$id.'_'.$name.'" type="'.$type.'" />
';
  }
  $ITEMS .= '</item>'.NL;
 }
$MRSS=<<<__MRSS
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss xmlns:media="http://search.yahoo.com/mrss" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>${logo}${prev}${next}
<generator>ppBlog $ppblog_version</generator>
<title>ppBlog-generated Image Gallery</title>
<link>$root</link>
<description>MRSS for Image Gallery</description>
$ITEMS
</channel>
</rss>

__MRSS;

 rewrite('feeds/gallery'.($i + 1).'.rss', $MRSS);
}

if(isset($images_cache) || !isset($DIVISION)) return;

return $DIVISION['body'] .= '<p class="alert-p">'.$pagesMRSS.'個のMRSSファイルが作成されました</p>
 <p class="center" style="margin-top: 1em;"><a href="'.(MODE_STATIC ? $root.'gallery/' : 'index.php?mode=gallery').'">[Image Gallery]</a></p>
';

?>